package org.lcogt.tools.fitsviewer.server;

import java.util.Comparator;

public class FitsComparator implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		//Try statement checks to see if the folder is a data (ex. 20090902)
		try{
			Integer.parseInt((String)o1);
			Integer.parseInt((String)o2);
			String one = (String)o1;
			String two = (String)o2;
			if(Integer.parseInt(one.substring(0,4)) == Integer.parseInt(two.substring(0,4)))
			{
				if(Integer.parseInt(one.substring(4,6))== Integer.parseInt(two.substring(4,6)))
					return Integer.parseInt(two.substring(6))- Integer.parseInt(one.substring(6));
				else
					return Integer.parseInt(two.substring(4,6))- Integer.parseInt(one.substring(4,6));
			}
			else
				return Integer.parseInt(one.substring(0,3))-Integer.parseInt(two.substring(0,3));
		} catch (NumberFormatException e){}//No catch statement needed just to test for date folder
		String[] firstParts = ((String) o1).split("_");
		String[] secondParts = ((String) o2).split("_");
		if(((String)o1).contains(".fits")&& !((String)o2).contains(".fits"))
			return -1;
		else if(!((String)o1).contains(".fits")&& ((String)o2).contains(".fits"))
			return 1;
		else if(!((String)o1).contains(".fits")&& !((String)o2).contains(".fits"))
			return ((String)o1).compareTo((String)o2);
		else if(Integer.parseInt(firstParts[3])-Integer.parseInt(secondParts[3])==0)
			return Integer.parseInt(firstParts[4])-Integer.parseInt(secondParts[4]);
		else
			return Integer.parseInt(firstParts[3])-Integer.parseInt(secondParts[3]);
	}

}
